import legacy from "@vitejs/plugin-legacy";
import vue from "@vitejs/plugin-vue";
import vueJsx from '@vitejs/plugin-vue-jsx'
import autoprefixer from "autoprefixer";
import path, { join } from "path";
import tailwindcss from "tailwindcss";
import AutoImport from "unplugin-auto-import/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import Components from "unplugin-vue-components/vite";
import { defineConfig } from "vite";
import compression from "vite-plugin-compression";
import Pages from "vite-plugin-pages";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
// https://vitejs.dev/config/
// window.location.origin + '/'
export default defineConfig({
    base:'/', 
    plugins: [
    vue(),
    vueJsx(),
    AutoImport({
      imports: [
        // 需要自动导入的插件，自定义导入的API
        "vue",
        "vue-router",
      ],
      dts: "src/types/auto-import.d.ts", // 指明 .d.ts 文件的位置和文件名
      resolvers: [ElementPlusResolver()],
      dirs: ["./src/api", "./src/utils", "./src/direct" ,'./src/request','./src/store'],
    }),
    Components({
      deep: true, //搜索子目录
      resolvers: [
        ElementPlusResolver(), //Element Plus按需加载
      ],
      dirs: ["src/components",'src/layouts'], //按需加载的文件夹
    }),
    createSvgIconsPlugin({
      iconDirs: [path.resolve(process.cwd(), "src/assets/svg")],
      symbolId: "icon-[name]",
      svgoOptions: {
        plugins: [
          {
            name: "removeAttrs",
            params: { attrs: ["class", "data-name", "fill", "stroke","stroke-width"] },
          },
        ],
      },
    }),
    Pages({
      // 需要生成路由的文件目录，默认就是识别src下面的pages文件
      pagesDir: join("./src/pages"),
      //dirs: "src/pages",
      // 排除在外的目录，即不将所有 components 目录下的 .vue 文件生成路由
      exclude: ["**/components/*"],
      importMode: "async",
    }),
    // Layouts({
    //     layoutsDirs: 'src/layouts', // 布局文件存放目录
    //     // defaultLayout: 'default', //对应 src/layouts/default.vue
    //  }),
    legacy({
      //需要兼容的目标列表，可以设置多个 'chrome 52'
      targets: ["defaults", "ie >= 11"],
    }),
    compression({
      verbose: true,
      disable: false,
      threshold: 10240, // 压缩阈值，小于这个值的文件将不会被压缩（单位为字节）这里就是大于 10kb 菜压缩
      algorithm: "gzip", // 压缩算法
      ext: ".gz", // 压缩文件后缀名
    }),
  ],
  resolve: {
    alias: {
      "@": path.resolve("./src"), //相对路径别名配置，使用@代替src
      com: path.resolve("./src/components"), //相对路径别名配置，使用@代替src
      Layouts: path.resolve(__dirname, "./src/layouts"),
      style: path.resolve(__dirname, "./src/assets/style"),
      images: path.resolve(__dirname, "./src/assets/images"),
      icon: path.resolve(__dirname, "./src/assets/icon"),
      router: path.resolve(__dirname, "./src/assets/router"),
      utils: path.resolve(__dirname, "./src/utils"),
      request: path.resolve(__dirname, "./src/request"),
      api: path.resolve(__dirname, "./src/api"),
      store: path.resolve(__dirname, "./src/store"),
      js: path.resolve(__dirname, "./src/assets/js"),
      
    },
  },
  css: {
    modules:true,
    postcss: {
      plugins: [tailwindcss, autoprefixer],
    },
    preprocessorOptions: {
      scss: {
        additionalData: `@import "./src/assets/style/value.scss";`,
      },
    },
    preprocessorMaxWorkers: true,
  },
  build: {
    assetsInlineLimit:4096,
    cssCodeSplit:true,
    terserOptions:{
        compress:{
            drop_console:true,
            drop_debugger:true,
        }
    },
    // rollupOptions: {
    //   output: {
    //     // 最小化拆分包
    //     manualChunks: (id) => {
    //       if (id.includes('node_modules')) {
    //         return id.toString().split('node_modules/')[1].split('/')[0].toString()
    //       }
    //     },
    //     // 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
    //     entryFileNames: 'js/[name].[hash].js',
    //     // 用于命名代码拆分时创建的共享块的输出命名
    //     chunkFileNames: 'js/[name].[hash].js',
    //     // 用于输出静态资源的命名，[ext]表示文件扩展名
    //     assetFileNames: '[ext]/[name].[hash].[ext]'
    //   }
    // }
  },
  server: {
    hmr: true, // 开启热更新
    port: 82,
    overlay: false, // 错误显示方式
    host: "0.0.0.0",
    proxy: {
      "/api": {
         target: "http://124.222.22.47:4025/",
       // target:'http://192.168.20.181:3025',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ""),
        ws:true,
      },
      
      
    },
    // watch:{ 
    //     ignored:['!**/node_modules/**']
    // }
  },
  clearScreen: false,
  extensions:['.vue','.js','.ts','.json','.css','.mjs','.mts']
});
